package com.example.coffeeseller_2.views.fragments;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.coffeeseller_2.R;
import com.example.coffeeseller_2.model.beans.ChooseCoffeeInfo;
import com.example.coffeeseller_2.model.beans.Coffee;
import com.example.coffeeseller_2.model.contentprovider.SingleMaterialList;
import com.example.coffeeseller_2.model.factory.BuyState;
import com.example.coffeeseller_2.model.factory.FragmentFactory;
import com.example.coffeeseller_2.model.helper.TradeAnimManager;
import com.example.coffeeseller_2.utils.ScreenUtil;
import com.example.coffeeseller_2.views.HomeActivity;
import com.example.coffeeseller_2.widget.CountDownBackView;
import com.example.coffeeseller_2.widget.KeyBoardView;
import com.example.coffeeseller_2.widget.KeyDisplayView;

import java.util.List;

/**
 * Created by WH on 2018/10/11.
 * 取杯码选择口味确定页面
 */

public class QubeimaTasteFragment extends BasicFragment {
    private static final String TAG = "QubeimaTasteFragment";
    private View content;
    private HomeActivity homeActivity;
    private Coffee coffee;
    private Handler handler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        content = super.onCreateView(inflater, container, FragmentFactory.getInstance().putLayoutId(R.layout.include_quebeima_taste));
        bindViews();
        initData();

       /* LinearLayout sure = (LinearLayout) content.findViewById(R.id.sure);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                qubeimaVerifySuccess();
            }
        });*/


        KeyBoardView keyBoardView = (KeyBoardView) content.findViewById(R.id.keboardview);
        final KeyDisplayView keyDisplayView = (KeyDisplayView) content.findViewById(R.id.keydispalyview);
        final CountDownBackView countDownBackView = (CountDownBackView) content.findViewById(R.id.countdownbackview);

        countDownBackView.startCount();

        keyBoardView.setOnKeyEventListener(new KeyBoardView.KeyEventListener() {
            @Override
            public void onKey(String key) {
                keyDisplayView.appendKey(key);
            }

            @Override
            public void delete() {
                keyDisplayView.removeLastKey();
            }
        });
        keyDisplayView.setOnKeyCompleteListener(new KeyDisplayView.KeyCompleteListener() {
            @Override
            public void onComplete() {
                Toast.makeText(homeActivity, "输入完成-" + keyDisplayView.getKeys(), Toast.LENGTH_SHORT).show();
                //验证取杯码是否有效,需要返回coffee数据

                //取杯码验证成功
                qubeimaVerifySuccess();
            }
        });
        countDownBackView.setOnCompleteListener(new CountDownBackView.CompleteListener() {
            @Override
            public void onFinish() {
                Toast.makeText(homeActivity, "倒计时结束", Toast.LENGTH_SHORT).show();

            }
        });

        return content;
    }

    private void bindViews() {

    }

    private void initData() {
        homeActivity = (HomeActivity) getActivity();
        handler = new Handler();
        coffee = null;

    }


    private void qubeimaVerifySuccess() {

        List<Coffee> coffees = SingleMaterialList.getInstance(homeActivity).getLastCoffeeList();
        coffee = coffees.get(0);

        if (coffee == null) {
            //如果验证成功后获取到的咖啡为null,需要提示“对不起，信息获取失败，请重试”

            Log.d(TAG, "coffee == null");

            return;
        }

        Log.d(TAG, "coffee != null");

        ChooseCoffeeInfo coffeeInfo = setChooseCoffeeInfo(coffee);
        if (coffeeInfo.isVilid()) {
            homeActivity.addTastPayFrag(coffeeInfo);
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    homeActivity.showTastPayFrag();
                }
            }, 250);


        }

    }

    private ChooseCoffeeInfo setChooseCoffeeInfo(Coffee coffee) {

        ChooseCoffeeInfo coffeeInfo = new ChooseCoffeeInfo();

        if (coffee.getImageBitmap() != null) {
            coffeeInfo.setCropImage(coffee.getImageBitmap());
        } else {
            Bitmap bitmap = BitmapFactory.decodeResource(homeActivity.getResources(), R.mipmap.no_coffee);
            coffeeInfo.setCropImage(bitmap);
        }
        int imgWidth = (int) homeActivity.getResources().getDimension(R.dimen.coffee_item_width);
        int imgHeight = (int) homeActivity.getResources().getDimension(R.dimen.coffee_item_height);
        coffeeInfo.setLocationX(ScreenUtil.getScreenWidth(homeActivity) / 2);
        coffeeInfo.setLocationX(ScreenUtil.getScreenHeight(homeActivity) / 2);

        Log.d(TAG, "width= " + ScreenUtil.getScreenWidth(homeActivity));
        Log.d(TAG, "height= " + ScreenUtil.getScreenHeight(homeActivity));
        coffeeInfo.setMinWidth((int) (imgWidth * TradeAnimManager.IMAGE_SCALE));
        coffeeInfo.setMinWidth((int) (imgHeight * TradeAnimManager.IMAGE_SCALE));
        coffeeInfo.setCoffee(coffee);
        coffeeInfo.setBuyState(BuyState.QUBEIMA_BUY);
        return coffeeInfo;
    }

}
